import java.sql.*;
import java.util.ResourceBundle;
import java.util.Scanner;

public class two {



    public static void main(String[] args) throws SQLException, ClassNotFoundException {
//        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver()); // 注册驱动

        ResourceBundle resourceBundle =  ResourceBundle.getBundle("jdbc");
        Class.forName(resourceBundle.getString("driverclass")); // 反射注册驱动

        String url = resourceBundle.getString("url");
        String password = resourceBundle.getString("password");
        String user =  resourceBundle.getString("user");
        Connection connection = DriverManager.getConnection(url, user, password);
        //链接数据库
//        System.out.println(connection);
//        String sql = "SELECT  *from tb_user where name = '" + user_name + "' and password = '" + user_password+ "'";
        String sql = "select *from tb_user where name = ? and password = ?";
        PreparedStatement PreparedStatement = connection.prepareStatement(sql);
//        Statement statement = connection.createStatement();// 获取数据库操作对象




// 执行sql
        Scanner scanner = new Scanner(System.in);
        System.out.println("账号:");
        String user_name = scanner.next();
        System.out.println("密码");
        String user_password = scanner.next();

        PreparedStatement.setString(1, user_name);
        PreparedStatement.setString(2, user_password);

        ResultSet resultSet = PreparedStatement.executeQuery();
        if (resultSet.next()) {

            System.out.println("登陆成功");
            System.out.println(resultSet.getString("name") + " " + resultSet.getString("password"));
        }
        else {
            System.out.println("登录失败");
        }

        // 关闭数据库
        if (PreparedStatement != null)
            PreparedStatement.close();
        if (connection != null)
            connection.close();


    }










}
